<template>
    <div class="qingwu">
        <div class="admin_table_page_title">
            <a-button @click="$router.back()" class="float_right" icon="arrow-left">返回</a-button>
            {{ edit ? '编辑楼栋' : '添加楼栋' }}
        </div>
        <div class="admin_form">
            <a-form-model :model="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }" ref="form">

                <a-form-model-item label="小区" :rules="{ required: true}">
                    <a-select  placeholder="请选择小区" v-model="form.community_id">
                        <a-select-option :value="value.community_id" v-for="(value, index) in communityList" :key="index">
                            {{value.community_name}}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>

                <a-form-model-item label="楼号" :rules="{ required: true}">
                    <a-input v-model="form.house" placeholder="请填写楼号，例如填写：3 代表三号楼，填写 1-5 则代表1到5号楼" />
                </a-form-model-item>

                <a-form-model-item :wrapper-col="{ span: 12, offset: 5 }">
                    <a-button type="primary" @click="handleSubmit" :loading="loading">提交</a-button>
                </a-form-model-item>
            </a-form-model>

        </div>
    </div>
</template>

<script>
const key = 'updatable';
export default {
    props: {},
    data() {
        return {
            form: {
                house: '',
                element: '',
                floor: '',
                room: '',
            },
            platform: false, // 平台PC false 手机 TRUE
            communityList: [],
            h_id: '',
            edit: false,
            loading: false,

        };
    },
    watch: {},
    computed: {},
    methods: {
        handleSubmit(formName) {
            this.loading = true;
            this.$message.loading({ content: 'Loading...', key });
            let api = this.$apiHandle(this.$api.estateHouse,this.h_id);
            if(api.status){
                this.$put(api.url,this.form).then(res=>{
                    this.loading = false;
                    if (res.code == 200) {
                        this.$message.success({ content: res.msg, key, duration: 2 })
                        return this.$router.back();
                    } else {
                        return this.$message.error(res.msg);
                    }
                })
            }else{
                this.$post(api.url,this.form).then(res=>{
                    this.loading = false;
                    if (res.code == 200) {
                        this.$message.success({ content: res.msg, key, duration: 2 })
                        return this.$router.back();
                    } else {
                        return this.$message.error(res.msg);
                    }
                })
            }
        },

        get_info() {
            this.$get(this.$api.estateHouse + '/' + this.h_id).then(res => {
                this.form.house = res.data.house_num;
                this.form.community_id = res.data.c_id;
            })
        },

        onload() {
            // 判断你是否是编辑
            if (!this.$isEmpty(this.$route.params.h_id)) {
                this.h_id = this.$route.params.h_id;
                this.get_info();
                this.edit = true
            }
        },

    },
    created() {
        this.onload();
    },
    mounted() {
        this.$get(this.$api.estateCommunity).then(res => {
            this.communityList = res.data.data;
        })
    }
};
</script>
